Skip to content

Method: buildAxiomValuesFromInstance(Object, AxiomValueGatherer)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.oom;
16:
17: import cz.cvut.kbss.jopa.exceptions.CardinalityConstraintViolatedException;
18: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
19: import cz.cvut.kbss.jopa.model.metamodel.Attribute;
20: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
21: import cz.cvut.kbss.ontodriver.model.Axiom;
22: import cz.cvut.kbss.ontodriver.model.NamedResource;
23: import cz.cvut.kbss.ontodriver.model.Value;
24:
25: class SingularDataPropertyStrategy<X> extends DataPropertyFieldStrategy<X> {
26:
27: Object value;
28:
29: SingularDataPropertyStrategy(EntityType<X> et, Attribute<? super X, ?> att,
30: Descriptor descriptor, EntityMappingHelper mapper) {
31: super(et, att, descriptor, mapper);
32: }
33:
34: @Override
35: void addValueFromAxiom(Axiom<?> ax) {
36: final Value<?> val = ax.getValue();
37: if (!isValidRange(val.getValue())) {
38: return;
39: }
40: verifyCardinalityConstraint(ax.getSubject());
41: this.value = val.getValue();
42: }
43:
44: void verifyCardinalityConstraint(NamedResource subject) {
45: if (value != null) {
46: throw new CardinalityConstraintViolatedException(
47: "Expected single value of attribute " + attribute.getName() + " of instance " + subject +
48: ", but got multiple");
49: }
50: }
51:
52: @Override
53: void buildInstanceFieldValue(Object entity) throws IllegalAccessException {
54: final Object toAssign = isFieldEnum() ? resolveEnumValue(value) : value;
55: setValueOnInstance(entity, toAssign);
56: }
57:
58: @Override
59: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) throws IllegalAccessException {
60: final Object extractedValue = extractFieldValueFromInstance(instance);
61:
62:• final Value<?> val = extractedValue != null ? new Value<>(extractedValue) : Value.nullValue();
63: valueBuilder.addValue(createAssertion(), val, getAttributeContext());
64: }
65: }